//
//  MessageViewController.m
//  ArtWalker
//
//  Created by Mac Jc on 15/8/3.
//  Copyright (c) 2015年 Mac Jc. All rights reserved.
//

#import "MessageViewController.h"
#import "TLSpringFlowLayout.h"
#import "MessageCollectionViewCell.h"
#import "InfoMationCollectionViewCell.h"
#import "MessageCollectionViewController.h"
#import "AllHeader.h"
#import "InfnMationCollectionViewController.h"
#import "MessageTalkViewController.h"
#import "ChatListViewController.h"
#import "SCHDStateViewController.h"
#import "STUDStateViewController.h"

@interface MessageViewController ()

/**
 *  导航栏按钮
 */
@property (nonatomic ,strong) UISegmentedControl *messageSegmentControl;

/**
 *  消息页面
 */
@property (nonatomic ,strong) MessageCollectionViewController *messageVC;

@property (nonatomic ,strong) ChatListViewController *chatView;

/**
 *  资讯页面
 */
@property (nonatomic ,strong) InfnMationCollectionViewController *infoMationVC;

//记录上一次显示的视图
@property (nonatomic ,strong) UIViewController *lastVC;

@end

@implementation MessageViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
//    self.navigationController.navigationBar.hidden = YES;
    
    self.view.backgroundColor = kBackgroundColor;

    [self creactInfoMationCollectionView];
    
    
    //设置导航栏
    [self navigationControllerVIew];
    //设置导航栏的阴影
    [self.navigationController.navigationBar dropShadowWithOffset:CGSizeMake(0, 1) radius:1 color:[UIColor blackColor] opacity:0.3];

    //设置导航栏的颜色
    self.navigationController.navigationBar.barTintColor = kCustomColor(255, 255, 255, 1.0);
}

#pragma mark - creactCollectionView
/**
 *  消息_back
 */
- (void)creactMessageCollectionView
{
    
}

/**
 *  消息
 */
- (void)creactChatListViewController
{
    //初始化当前类，并设置需要显示的会话类型和聚合会话类型
    _chatView = [[ChatListViewController alloc] initWithDisplayConversationTypes:@[@(ConversationType_PRIVATE)] collectionConversationType:nil];
    [self addChildViewController:_chatView];
    [self.chatView.view setFrame:self.view.frame];
    _chatView.view.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 113);
    
    _chatView.view.backgroundColor = kBackgroundColor;
    //设置头像为圆角边型
    [_chatView setConversationAvatarStyle:RC_USER_AVATAR_CYCLE];
    //设置头像大小
    [_chatView setConversationPortraitSize:CGSizeMake(40, 40)];
    
    __block MessageViewController *messageView = self;
    //进入会话页面
    _chatView.pushChatListView = ^(RCConversationModel *model)
    {
        RCConversationViewController *conversationVC = [[RCConversationViewController alloc]init];
        //隐藏tabbar按钮
        messageView.hidesBottomBarWhenPushed = YES;
        conversationVC.view.backgroundColor = kBackgroundColor;
        conversationVC.conversationType = model.conversationType;  //  会话类型
        conversationVC.targetId = model.targetId;  //  会话 Id
        conversationVC.userName = model.conversationTitle;  //  会话名称
        conversationVC.title = model.conversationTitle;  //  会话名称
        [messageView.navigationController pushViewController:conversationVC animated:YES];
        //返回页面重新显示tabbar按钮
        messageView.hidesBottomBarWhenPushed = NO;
        //刷新会话列表
        [messageView.chatView refreshConversationTableViewIfNeeded];
    };
    [self.view addSubview:_chatView.view];
    
}

/**
 *  资讯
 */
- (void)creactInfoMationCollectionView
{
    //初始化UICollectionViewFlowLayout
    UICollectionViewFlowLayout *Layout = [[UICollectionViewFlowLayout alloc] init];
    _infoMationVC = [[InfnMationCollectionViewController alloc] initWithCollectionViewLayout:Layout];
    [self addChildViewController:_infoMationVC];
    
    self.infoMationVC.collectionView.alwaysBounceVertical = YES;
    //设置上下间距
    Layout.minimumInteritemSpacing = 0;
    //设置左右间距
    Layout.minimumLineSpacing = 8;
    Layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH - 16, 10);
    Layout.itemSize = CGSizeMake(SCREEN_WIDTH - 16, 70);
    [self.view addSubview:_infoMationVC.view];
    
    __weak MessageViewController *messAgeView = self;
    _infoMationVC.pushInfoView = ^(InfoMactionModel *model)
    {
        SCHDStateViewController *state = [[SCHDStateViewController alloc] init];
        state.ID = model.Id;
        [messAgeView.navigationController pushViewController:state animated:YES];
        messAgeView.hidesBottomBarWhenPushed = NO;
    };
    //设置大小
    _infoMationVC.view.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 79);
    _infoMationVC.navigationController.navigationBar.tintColor = kCustomColor(50, 50, 70, 1);
    
}

/**
 *  重构导航栏
 *
 *  @return 导航栏视图
 */
- (void)navigationControllerVIew
{
    //获取导航栏的标题
    NSArray *segmentArr = @[@"咨询",@"消息"];
    //加入到UISegmentControl
    _messageSegmentControl = [[UISegmentedControl alloc] initWithItems:segmentArr];
    //设置选中的颜色
    _messageSegmentControl.tintColor = [UIColor colorWithRed: 0.1569 green: 0.1569 blue: 0.1961 alpha: 1.0];
    //设置UISegmentControl的大小
    _messageSegmentControl.frame = CGRectMake(10, 5, SCREEN_WIDTH - 200, 30);
    //设置点击UISegmentControl的事件
    [_messageSegmentControl addTarget:self action:@selector(tapSegment:) forControlEvents:UIControlEventValueChanged];
    //默认选择的下标为第一个
    [_messageSegmentControl setSelectedSegmentIndex:0];
    
    self.navigationItem.titleView = _messageSegmentControl;
    [self creactInfoMationCollectionView];
    
    
    //设置用户信息提供者,页面展现的用户头像及昵称都会从此代理取
    [[RCIM sharedRCIM] setUserInfoDataSource:self];
    
    //自定义导航左右按钮
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"单聊" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemPressed:)];
    [rightButton setTintColor:[UIColor blackColor]];
    self.navigationItem.rightBarButtonItem = rightButton;
}

/**
 *  重载右边导航按钮的事件
 *
 *  @param sender <#sender description#>
 */
-(void)rightBarButtonItemPressed:(id)sender
{
    
    self.hidesBottomBarWhenPushed = YES;
    RCConversationViewController *conversationVC = [[RCConversationViewController alloc]init];
    //这个类型表示是私聊
    conversationVC.conversationType = ConversationType_PRIVATE;
    conversationVC.targetId = @"1"; //这里模拟自己给自己发消息，您可以替换成其他登录的用户的UserId
    conversationVC.userName = @"测试2";
    conversationVC.title = @"自问自答";
    [self.navigationController pushViewController:conversationVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

/**
 *此方法中要提供给融云用户的信息，建议缓存到本地，然后改方法每次从您的缓存返回
 */
- (void)getUserInfoWithUserId:(NSString *)userId completion:(void(^)(RCUserInfo* userInfo))completion
{
    //此处为了演示写了一个用户信息
    if ([@"1" isEqual:userId]) {
        RCUserInfo *user = [[RCUserInfo alloc]init];
        user.userId = @"1";
        user.name = @"测试1";
        user.portraitUri = @"https://ss0.baidu.com/73t1bjeh1BF3odCf/it/u=1756054607,4047938258&fm=96&s=94D712D20AA1875519EB37BE0300C008";
        
        return completion(user);
    }
}

/**
 *  点击UISegmentedControl执行的方法
 *
 *  @param index UISegmentedControl参数下标
 */
- (void)tapSegment:(UISegmentedControl *)index
{
    if(index.selectedSegmentIndex == 0)
    {
        [_chatView.view removeFromSuperview];
        [_chatView removeFromParentViewController];
        //调用资讯页面
        [self creactInfoMationCollectionView];
    }
    else
    {
        [_infoMationVC.view removeFromSuperview];
        [_infoMationVC removeFromParentViewController];
        //调用消息页面
        [self creactChatListViewController];
    }
}

@end
